View Javadoc

1   package uba.db.impl.filesystem;
2   
3   import java.io.DataInputStream;
4   import java.io.EOFException;
5   import java.io.FileInputStream;
6   import java.io.FileNotFoundException;
7   import java.io.IOException;
8   
9   import uba.db.table.Row;
10  import uba.db.table.io.TableReaderCloseException;
11  import uba.db.table.io.FetchRowException;
12  import uba.db.table.io.NoMoreRowsException;
13  import uba.db.table.io.RowReader;
14  import uba.db.table.io.TableReader;
15  import uba.db.table.io.TableReaderCreationException;
16  
17  /***
18   * @version $Revision: 1.3 $
19   */
20  public class FileSystemTableReader implements TableReader {
21      private FileInputStream fileInput;
22      private RowReader rowReader;
23      private Row nextRow;
24      private boolean hasMoreRows;
25  
26      public FileSystemTableReader(FileSystemTable fileSystemTable)
27              throws TableReaderCreationException {
28          initializeFileInputFor(fileSystemTable);
29          rowReader = new RowReader(fileSystemTable, new DataInputStream(fileInput));
30          fetchNextRow();
31      }
32  
33      private void initializeFileInputFor(FileSystemTable fileSystemTable) {
34          try {
35              fileInput = new FileInputStream(fileSystemTable.dataFile());
36          } catch (FileNotFoundException e) {
37              throw new TableReaderCloseException(e);
38          }
39      }
40  
41      private void fetchNextRow() {
42          try {
43              nextRow = rowReader.read();
44              hasMoreRows = true;
45          } catch (EOFException e) {
46              hasMoreRows = false;
47          } catch (IOException e) {
48              throw new FetchRowException(e);
49          }
50      }
51  
52      /***
53       * @see uba.db.table.io.TableReader#hasMoreRows()
54       */
55      public boolean hasMoreRows() {
56          return hasMoreRows;
57      }
58  
59      /***
60       * @see uba.db.table.io.TableReader#fetchRow()
61       */
62      public Row fetchRow() throws FetchRowException {
63          if (!hasMoreRows) {
64              throw new NoMoreRowsException();
65          }
66  
67          Row currentRow = nextRow;
68          fetchNextRow();
69          return currentRow;
70      }
71  
72      /***
73       * @see uba.db.table.io.TableReader#close()
74       */
75      public void close() throws TableReaderCloseException {
76          try {
77              fileInput.close();
78          } catch (IOException e) {
79              throw new TableReaderCloseException(e);
80          }
81      }
82  }